package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.Authority;
import com.xj.edu.hotel.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;
    /**
     * 查看各类角色的权限
     */
    @RequestMapping("findRolePermission")
    public String findRolePermission(Model model){
        List<Authority> authorities = authorityService.findRolePermission();
        model.addAttribute("authorities",authorities);
        return "manager-permission";
    }


    /**
     * 查找所有的权限
     */
    @RequestMapping("findAllPermission")
    @ResponseBody
    public List<Authority> findAllPermission(){
        List<Authority> authorities = authorityService.findAllPermission();
        return authorities;
    }
    /**
     * 通过权限查找权限描述
     */
    @RequestMapping("findPermissionDescription")
    @ResponseBody
    public Authority findPermissionByDescription(String permissionType){
        return authorityService.findPermissionByDescription(permissionType);
    }
    /**
     * 修改角色权限
     */
    @RequestMapping("updateRolePermission")
    public String updateRolePermission(String authorityId,String roleId){
        Authority authority = new Authority();
        authority.setAuthorityId(authorityId);
        authority.setRoleId(roleId);

        int success = authorityService.updateRolePermission(authority);
        if (success > 0) {
            return "redirect:/permission/findRolePermission";
        }else {
            return "error";
        }
    }

}
